//
//  ControlTemplateViewTextViewController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewTextViewController.h"

#pragma mark -
#pragma mark 多行文本框控件【textarea】
@interface ControlTemplateViewTextViewController ()
{
    UILabel* showContentlabel;
    CustomButtonView* abtn;
}
@property (nonatomic, strong) NSString* defaultValue;
@end

@implementation ControlTemplateViewTextViewController

static ControlTemplateViewTextViewController* sharedManagerTextViewInter = nil;

+ (ControlTemplateViewTextViewController*)sharedManagerTextView
{
    sharedManagerTextViewInter = [[self alloc] init];
    
    return sharedManagerTextViewInter;
}

/**
 *  初始化变量
 */
- (void)initVar
{
    _valueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.isNeedAdpterHotspot = YES;
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 120)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    [self initVar];
    
    maxString = [[_params valueForKey:@"contentLengthLimit"] integerValue];
    
    UILabel* titleLabel = [[UILabel alloc] init];
    [titleLabel setText:[_params valueForKey:@"title"]];
    [titleLabel setFont:kFont_16];
    [titleLabel setTextColor:kColor_1];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgMainView.mas_left).offset(HEADSPACE);
        make.top.equalTo(_bgMainView.mas_top).offset(HEADSPACE);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    
    ////////判断defaultValue是否有值
    if ([[_params valueForKey:@"attrValue"] length] > 0) {
        _defaultValue = [_params valueForKey:@"attrValue"];
    }
    
    ////////是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(LINEMARGIN_LEFT, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    CGFloat height = 0;
    
    ////////是否可写 //是否可读 0-不可写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) //当writable为0时可读
    {
        NSString* valueStr = [_params valueForKey:@"attrValue"];
        CGSize size = STRING_SIZE_FONT(CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT - 10, valueStr, 16);
        CGFloat lineNum = size.height / 16; //计算总行数
        if (lineNum > 5) {
            height = 80;
        }
        else {
            if (size.height > 50) {
                height = size.height;
            }
            else {
                height = 50;
            }
        }
        
        _bgMainView.frame = CGRectMake(0, _bgMainView.frame.origin.y, CGRectGetWidth(_bgMainView.bounds), height + 20);
        
        showContentlabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, (CGRectGetHeight(_bgMainView.bounds) - height) / 2, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT - 10, height)];
        [showContentlabel setText:valueStr];
        showContentlabel.lineBreakMode = NSLineBreakByCharWrapping;
        showContentlabel.numberOfLines = 0;
        [showContentlabel setFont:kFont_16];
        [showContentlabel setTextColor:kColor_1];
        [_bgMainView addSubview:showContentlabel];
        
        
        if (lineNum > 5) //判断行数
        {
            _bgMainView.frame = CGRectMake(0, _bgMainView.frame.origin.y, CGRectGetWidth(_bgMainView.bounds), height + 40);
            //下拉箭头
            abtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT - 10, height + 15, 50, 20)];
            [abtn.titleLabel setFont:kFont_16];
            [abtn addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [abtn setTitle:@"全文" forState:UIControlStateNormal];
            [abtn setTitleColor:kColor_S3 forState:UIControlStateNormal];
            [_bgMainView addSubview:abtn];
        }
    }
    else {
        contentText = [[BaseTextView alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT - 5, 5, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT + 5, 110)];
        [contentText setTextColor:kColor_1];
        [contentText setFont:kFont_16];
        [_bgMainView addSubview:contentText];
        contentText.delegate = self;
        
        //提示文本
        CGSize size = STRING_SIZE_FONT(CGRectGetWidth(contentText.bounds) - 10, [_params valueForKey:@"markedWords"], 16);
        msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, CGRectGetWidth(contentText.bounds) - 10, size.height)];
        msgLabel.lineBreakMode = NSLineBreakByCharWrapping;
        msgLabel.numberOfLines = 0;
        [msgLabel setText:[_params valueForKey:@"markedWords"]];
        [msgLabel setFont:kFont_16];
        [msgLabel setTextColor:kColor_C];
        [contentText addSubview:msgLabel];
        
        contentText.inputAccessoryView = [self onKeyBoardFinishedButton];
        contentText.text = _defaultValue;
        [self textViewDidChange:contentText];
    }
    
    //标题长度换行
    //    CGSize sizeTitle = STRING_SIZE_FONT(TITLE_WIDTH, [_params valueForKey:@"title"], kFont_16);
    //    titleLabel.frame = CGRectMake(20, (CGRectGetHeight(_bgMainView.bounds)-sizeTitle.height)/2, TITLE_WIDTH, sizeTitle.height);
    
    //    CGSize sizeContent = STRING_SIZE_FONT(CGRectGetWidth(contentText.bounds), [contentText text], kFont_16);
    
    /**
     *  @author 肖湘军, 16-12-31 16:12:35
     *
     *  不可写的时候进行
     */
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) {
        if (height <= 50) //当writable为0时可读
        {
            titleLabel.frame = CGRectMake(titleLabel.frame.origin.x,
                                          0,
                                          CGRectGetWidth(titleLabel.bounds), 50);
            
            contentText.frame = CGRectMake(contentText.frame.origin.x,
                                           0,
                                           CGRectGetWidth(contentText.bounds),
                                           50);
            showContentlabel.frame = CGRectMake(showContentlabel.frame.origin.x,
                                                0,
                                                CGRectGetWidth(showContentlabel.bounds),
                                                50);
            _bgMainView.frame = CGRectMake(_bgMainView.frame.origin.x,
                                           _bgMainView.frame.origin.y,
                                           CGRectGetWidth(_bgMainView.bounds),
                                           50);
        }
    }
    
    UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
    lineView.frame = CGRectMake(lineView.frame.origin.x,
                                CGRectGetHeight(_bgMainView.bounds) - 0.5,
                                CGRectGetWidth(lineView.bounds),
                                CGRectGetHeight(lineView.bounds));
}



/**
 *  给多行文本控件填充值
 *
 *  @param valueStr 接收字符串值
 */
- (void)paddingTextView:(NSString*)valueStr
{
    [contentText setText:valueStr];
    // [需求12430]属性为必填时需要校验是否为空格
    NSString *fillterValueStr = [valueStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([fillterValueStr length] > 0) {
        _valueStr = valueStr;
        [msgLabel setHidden:YES];
        _optional = @"0";
    }
    else {
        [msgLabel setHidden:NO];
        _optional = @"1";
    }
}

/**
 *  当数据超过5行的时候，进行缩成2行，点击下接箭头可以显示全部内容
 *
 *  @param sender 按钮实例
 */
- (void)onBtnAction:(CustomButtonView*)sender
{
    UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
    
    NSString* valueStr = [_params valueForKey:@"attrValue"];
    CGSize size = STRING_SIZE_FONT(CGRectGetWidth(_bgMainView.bounds) - CONTENT_WIDTH - HEADSPACE - 10, valueStr, 16);
    
    if (![[[sender titleLabel] text] isEqualToString:@"收起"]) {
        [sender setTitle:@"收起" forState:UIControlStateNormal];
        showContentlabel.frame = CGRectMake(showContentlabel.frame.origin.x,
                                            showContentlabel.frame.origin.y,
                                            CGRectGetWidth(showContentlabel.bounds),
                                            size.height);
        _bgMainView.frame = CGRectMake(0, _bgMainView.frame.origin.y, CGRectGetWidth(_bgMainView.bounds), size.height + 40);
        abtn.frame = CGRectMake(CONTENTMARGIN_LEFT - 10, size.height + 15, 50, 20);
    }
    else {
        [sender setTitle:@"全文" forState:UIControlStateNormal];
        showContentlabel.frame = CGRectMake(showContentlabel.frame.origin.x,
                                            showContentlabel.frame.origin.y,
                                            CGRectGetWidth(showContentlabel.bounds),
                                            80);
        _bgMainView.frame = CGRectMake(0, _bgMainView.frame.origin.y, CGRectGetWidth(_bgMainView.bounds), 120);
        abtn.frame = CGRectMake(CONTENTMARGIN_LEFT - 10, 95, 50, 20);
    }
    
    lineView.frame = CGRectMake(lineView.frame.origin.x,
                                CGRectGetHeight(_bgMainView.bounds) - 0.5,
                                CGRectGetWidth(lineView.bounds),
                                CGRectGetHeight(lineView.bounds));
    //改变布局
    [self.fundelegate onFeeRefeshUI];
}


/**
 *  收键盘
 *
 *  @param sender 按钮实例
 */
- (void)onKeyBoardFinishedButton:(CustomButtonView*)sender
{
    [contentText resignFirstResponder];
    //    NSLog(@"键盘收起时多行文本框高度 %@",NSStringFromCGRect(_bgMainView.bounds));
}

- (void)textViewDidBeginEditing:(UITextView*)textView
{
    //    NSLog(@"开始编辑时文本框高度 %@",NSStringFromCGRect(textView.bounds));
    [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardDidShowNotification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:textView, @"object", nil]];
}
- (void)textViewDidEndEditing:(UITextView*)textView
{
    
    //     NSLog(@"结束编辑时文本框高度 %@",NSStringFromCGRect(textView.bounds));
    [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardDidHideNotification object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:textView, @"object", nil]];
}

/**
 *  当多行文本内容有变化，进行回调
 *
 *  @param textView 文本实例
 */
- (void)textViewDidChange:(BaseTextView*)textView
{
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    if ([[textView text] length] > 0) {
        [msgLabel setHidden:YES];
        // [需求12430]属性为必填时需要校验是否为空格
        NSString *fillterValueStr = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if (fillterValueStr.length > 0) {
            _optional = @"0";
        }
    }
    else {
        [msgLabel setHidden:NO];
    }
    
    if ([[textView text] length] > maxString) {
        NSString* contentStr = [[textView text] substringToIndex:maxString];
        textView.text = contentStr;
    }
    
    _valueStr = [textView text];
}

/**
 *  当多行文本内容快要结束时，进行回调
 *
 *  @param textView 文本实例
 *
 *  @return 返回YES值
 */
- (BOOL)textViewShouldEndEditing:(BaseTextView*)textView
{
    [contentText resignFirstResponder];
    return YES;
}

@end

